// Server Side

// Created timer limit script if not made already.
local DefaultTimerLimit = "600"
local DefaultTimer = "1"
if (not file.IsDir("TDM/Timer","DATA")) then
    file.CreateDir("TDM/Timer")
end
if not(file.Exists("TDM/Timer/TimerLimit.txt","DATA")) then
	file.Write("TDM/Timer/TimerLimit.txt", DefaultTimerLimit)
	print("[TDM] Wrote file : TDM/Timer/TimerLimit.txt")
end
if (not file.IsDir("TDM/Timer","DATA")) then
    file.CreateDir("TDM/Timer")
end
if not(file.Exists("TDM/Timer/Timer.txt","DATA")) then
	file.Write("TDM/Timer/Timer.txt", DefaultTimer)
	print("[TDM] Wrote file : TDM/Timer/Timer.txt")
end

// Restarts the server if the timer reaches SPECIFIC TIME
timer.Create("OfficialCountDownTimer",1,0, function()
	if tonumber(file.Read("TDM/Timer/Timer.txt","DATA")) == 1 then
	umsg.Start("TDMGETIFTIMER")
	umsg.Short(tonumber(file.Read("TDM/Timer/Timer.txt","DATA")))
	umsg.Short(tonumber(file.Read("TDM/Timer/TimerLimit.txt","DATA")))
	umsg.End()
		if tonumber(file.Read("TDM/Timer/TimerLimit.txt","DATA") - math.Round(CurTime())) <= 0 then
			game.ConsoleCommand("changelevel "..game.GetMap( ).."\n")
		end
	else
	umsg.Start("TDMGETIFTIMER")
	umsg.Short(tonumber(file.Read("TDM/Timer/Timer.txt","DATA")))
	umsg.Short(tonumber(file.Read("TDM/Timer/TimerLimit.txt","DATA")))
	umsg.End()
	end
		local GetPlayers = player.GetAll()
	for k,v in pairs(GetPlayers) do
		if v:IsValid() then
			v:SetPData("TDM_Total_Score_Ok", 0)
			v:SetPData("MyTimerLimiter", 0)
			umsg.Start("PlaceScoreForMenu")
				umsg.Short(k)
				umsg.Short(file.Read("TDM/GoalLimit/GoalLimit.txt"))
			umsg.End()
			
			umsg.Start("TimeLimitUnOfficialMenu")
				umsg.String(file.Read("TDM/Timer/TimerLimit.txt","DATA") / 60)
				umsg.String(k)
			umsg.End()
			
			umsg.Start("LeaderBoardRankName")
				umsg.String(k)
				umsg.String(v:GetPData('MyTDMRank'))
			umsg.End()
		end
	end
end)

// The Timer Button
function TDMTimerButton( ply, cmd, args )
if ply:IsSuperAdmin() then
if (not file.IsDir("TDM/Timer","DATA")) then
    file.CreateDir("TDM/Timer")
end
if not(file.Exists("TDM/Timer/Timer.txt","DATA")) then
	file.Write("TDM/Timer/Timer.txt", tonumber(args[1]))
	print("[TDM] Wrote file : TDM/Timer/Timer.txt")
end
	file.Write("TDM/Timer/Timer.txt", tonumber(args[1]))
if tonumber(args[1]) == 1 then
TimerChk = "on"
else
TimerChk = "off"
end
umsg.Start( "TDM_Changed_Timer" )
umsg.String(ply:Nick())
umsg.Short(ply:Team())
umsg.String(TimerChk)
umsg.End() 
file.Append( "TDM/Logs/"..os.date("%m-%d-%y")..".txt", os.date("%I:%M:%S").." | "..ply:Nick().." has turned the timer "..TimerChk.." \n" )
else
umsg.Start( "TDM_Not_Admin", ply )
umsg.End()
end
end
concommand.Add( "TDMTimerOn", TDMTimerButton )


function TDMCustomTimerLimiterChk(ply, cmd, args) -- Basically had to add it twice due to a glitch with [1] and shortcuts
local oargs = tonumber(args)
local args = tonumber(args) * 60
if type(args) != "number" then 
umsg.Start( "TDM_Timer_Not_Number", ply )
umsg.End() 
return end
if args <= 0 then 
umsg.Start( "TDM_Timer_Is_Zero", ply )
umsg.End()
return end
if args > math.floor(args) then 
umsg.Start( "TDM_Timer_Has_Decimal", ply )
umsg.End()
return end
if ply:IsSuperAdmin() then
	if (not file.IsDir("TDM/Timer","DATA")) then
		file.CreateDir("TDM/Timer")
	end
	if not(file.Exists("TDM/Timer/TimerLimit.txt","DATA")) then
		file.Write("TDM/Timer/TimerLimit.txt", args)
		print("[TDM] Wrote file : TDM/Timer/TimerLimit.txt")
	end
		file.Write("TDM/Timer/TimerLimit.txt", args)
	umsg.Start( "TDM_Changed_Timer_Limit" )
		umsg.String(ply:Nick())
		umsg.Short(ply:Team())
		umsg.String(oargs)
	umsg.End() 
	file.Append( "TDM/Logs/"..os.date("%m-%d-%y")..".txt", os.date("%I:%M:%S %p").." | "..ply:Nick().." has set the Timer to: "..oargs.." minutes\n" )
else
umsg.Start( "TDM_Not_Admin", ply )
umsg.End() 
end
end


// The Timer Set Button
function TDMCustomCountDownTimer( ply, cmd, args )
if ply:IsSuperAdmin() then
local args = tonumber(args[1])
if type(args) != "number" then 
umsg.Start( "TDM_Timer_Not_Number", ply )
umsg.End() 
return end
if args <= 0 then 
umsg.Start( "TDM_Timer_Is_Zero", ply )
umsg.End()
return end
if args > math.floor(args) then 
umsg.Start( "TDM_Timer_Has_Decimal", ply )
umsg.End()
return end
TDMCustomTimerLimiterChk(ply, cmd, args)
else
umsg.Start( "TDM_Not_Admin", ply )
umsg.End() 
end
end
concommand.Add( "TDMCustomCountDownTimer", TDMCustomCountDownTimer )